package com.zerro.litez.runtime;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@ConfigurationProperties(prefix = "litez")
public final class LitEzProperties implements EnvironmentAware {

    private Class<?> driver;

    private String driver_;

    private String url;

    private String username;

    private String password;


    private static LitEzProperties prop;

    public LitEzProperties() {

    }


    public Class<?> getDriver() {
        return driver;
    }

    public void setDriver(Class<?> driver) {
        this.driver = driver;
    }

    public String getDriver_() {
        return driver_;
    }

    private void setDriver_(String driver_) {
        this.driver_ = driver_;
        isValueNull("driver", driver_);
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
        isValueNull("url", url);
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
        isValueNull("username", username);
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        isValueNull("password", password);
    }

    @Override
    public String toString() {
        return "LitEzProperties{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", user='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    @Override
    public void setEnvironment(Environment environment) {
        setDriver_(environment.getProperty("litez.driver"));
        setUrl(environment.getProperty("litez.url"));
        setUsername(environment.getProperty("litez.username"));
        setPassword(environment.getProperty("litez.password"));
        prop = this;
    }

    public static LitEzProperties getProp() {
        throwNullValues();
        return prop;
    }

    static List<String> valueNames = new ArrayList<>();

    private static void isValueNull(String name, String value) {
        if (value == null || value.isEmpty()) {
            valueNames.add(name);
        }
    }

    private static void throwNullValues() {
        if (valueNames.isEmpty()) {
            return;
        }
        StringBuilder values = new StringBuilder();
        for (int i = 0; i < valueNames.size(); i++) {
            String name = valueNames.get(i);
            values.append("litez.")
                    .append(name);
            if (i < valueNames.size() - 1) {
                values.append(", ");
            }
        }
        throw new IllegalArgumentException("必须为LitEz设置以下属性：" + values);
    }
}


